﻿#include "hotelmanage.h"
#include "ui_hotelmanage.h"
#include <QMessageBox>

HotelManage::HotelManage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HotelManage)
{
    ui->setupUi(this);

    for (int i = 0; i < 2; i++)
    {
        m_contentCount[i] = 0;
    }
    m_registerHotel = nullptr;
    m_houseManage = nullptr;

    ui->tabWidget->setCurrentIndex(0);
}

HotelManage::~HotelManage()
{
    delete ui;
}

void HotelManage::slotHotelManageListClean()
{
    for (int i = 0; i < 2; i++)
    {
        if (m_contentCount[i] == 0)
        {
            continue;
        }
        else
        {
            m_contentCount[i] = 0;
            m_hotelVector[i].clear();
        }
    }

    while(ui->lwg_passing->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_passing->takeItem(0);
        delete item;
    }
    while(ui->lwg_pass->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_pass->takeItem(0);
        delete item;
    }
    ui->lb_hotelCount->setText("0");
}

void HotelManage::slotSetHotelInfosList(bool result, const QString& msg)
{
    qDebug() << "slotSetHotelInfosList " << "result = " << result;
    if (result)
    {
        HotelManageContent *hotelContent = new HotelManageContent();
        if (msg == "已审核")
        {
            m_hotelVector[0].append(hotelContent);
            ui->lwg_pass->addItem("");
            ui->lwg_pass->setItemWidget(ui->lwg_pass->item(m_contentCount[0]),hotelContent);
            ui->lwg_pass->item(m_contentCount[0])->setSizeHint(QSize(0, 120));  //每次改变Item的高度
            m_contentCount[0]++;
        }
        else{
            m_hotelVector[1].append(hotelContent);
            ui->lwg_passing->addItem("");
            ui->lwg_passing->setItemWidget(ui->lwg_passing->item(m_contentCount[1]),hotelContent);
            ui->lwg_passing->item(m_contentCount[1])->setSizeHint(QSize(0, 120));  //每次改变Item的高度
            m_contentCount[1]++;
        }

        connect(hotelContent,SIGNAL(signalWatchHotelInfosDetails(const QString &)),
                this, SIGNAL(signalGainHotelDetails(const QString&)));
        connect(hotelContent,SIGNAL(signalWatchHouseTypeInfos(const QString &, const QString &)),
                this, SLOT(slotGainHouseList(const QString&, const QString &)));
        connect(hotelContent,SIGNAL(signalDeleteHotelInfos(const QString &)),
                this, SIGNAL(signalDeleteHotelInfo(const QString&)));

        ui->lb_hotelCount->setText(QString::number(m_contentCount[0] + m_contentCount[1]));
    }
    else
    {
        qDebug() << msg;
    }
}

void HotelManage::slotSetHotelInfosImage(bool result,const QString& hotelID,const QImage& image)
{
    if (result)
    {
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < m_contentCount[i]; j++)
            {
                if (hotelID == m_hotelVector[i][j]->getHotelID())
                {
                    m_hotelVector[i][j]->setImage(image);
                }
            }
        }
    }
}


void HotelManage::on_pb_addHotel_clicked()
{
    if (m_registerHotel != nullptr)
    {
        delete m_registerHotel;
    }

    m_registerHotel = new RegisterHotel();
    m_registerHotel->setState(E_NewCreate);
    m_registerHotel->setWindowTitle("新增门店");
    m_registerHotel->setWindowModality(Qt::ApplicationModal);
    m_registerHotel->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
    m_registerHotel->setFixedSize(m_registerHotel->width(),m_registerHotel->height());               // 禁止拖动窗口大小
    m_registerHotel->show();

    connect(m_registerHotel,SIGNAL(signalSubmitHotel()),
            this,SIGNAL(signalSubmitHotelInfo()));
}

void HotelManage::slotSetHotelDetails()
{
    if (m_registerHotel != nullptr)
    {
        delete m_registerHotel;
    }

    m_registerHotel = new RegisterHotel();
    m_registerHotel->setHotelInfo();
    m_registerHotel->setWindowTitle("门店详情");
    m_registerHotel->setState(E_WatchDetails);
    m_registerHotel->setWindowModality(Qt::ApplicationModal);
    m_registerHotel->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
    m_registerHotel->setFixedSize(m_registerHotel->width(),m_registerHotel->height());   // 禁止拖动窗口大小
    m_registerHotel->show();

    connect(m_registerHotel,SIGNAL(signalUpdateHotel()),
            this,SIGNAL(signalSubmitHotelInfo()));
}

void HotelManage::slotDeleteHotelInfoResult()
{

}

void HotelManage::slotHotelImage()
{
    m_registerHotel->setImages();
}

void HotelManage::slotSetHouseList(bool result,QString msg)
{
    if (result)
    {
        m_houseManage->setHouseList();
    }
    else
    {
        qDebug() << msg;
    }
}

void HotelManage::slotGainHouseList(const QString& index , const QString & hotelID)
{
    if (m_houseManage != nullptr)
    {
        delete m_houseManage;
    }
    m_houseManage = new HouseManage();
    m_houseManage->setHotelID(hotelID);
    m_houseManage->setWindowTitle("房间类型详情");
    m_houseManage->setWindowModality(Qt::ApplicationModal);
    m_houseManage->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
    m_houseManage->setFixedSize(m_houseManage->width(),m_houseManage->height());   // 禁止拖动窗口大小
    m_houseManage->show();

    connect(m_houseManage,SIGNAL(signalDeleteHouseType(const QString&)),
            this,SIGNAL(signalDeleteHouseInfo(const QString&)));
    connect(m_houseManage,SIGNAL(signalSubmitHouseType()),
            this,SIGNAL(signalSubmitHouseInfo()));
    emit signalGainHouseTypeInfo(index, hotelID);
}

void HotelManage::slotSaveHouseImage(const QString& houseType,const QImage& image)
{
    m_houseManage->saveHouseImage(houseType,image);
}

void HotelManage::slotSubmitHouseResult(bool result,const QString& msg)
{
    if (result)
    {
        emit signalGainHouseTypeInfo("0", m_houseManage->getHotelID());
        m_houseManage->cleanHouseInfoList();
    }
    m_houseManage->setSubmitHouseResult(result,msg);
}
void HotelManage::slotDeleteHouseResult(bool result,const QString& msg)
{
    if (result)
    {
        emit signalGainHouseTypeInfo("0", m_houseManage->getHotelID());
        m_houseManage->cleanHouseInfoList();
    }
   else
    {
        m_houseManage->errorShow(msg);
    }
}
void HotelManage::slotSubmitHotelResult(bool result,const QString& msg)
{
    if (result)
    {
        m_registerHotel->close();
        emit signalGainHotelTypeInfo();
    }
    else
    {
        QMessageBox::warning(m_registerHotel, "错误", msg,  QMessageBox::Yes, QMessageBox::Yes);
    }
}
void HotelManage::slotDeleteHotelResult(bool result,const QString& msg)
{
    if (result)
    {
        emit signalGainHotelTypeInfo();
    }
   else
    {
        QMessageBox::warning(this, "错误", msg,  QMessageBox::Yes, QMessageBox::Yes);
    }
}
